package com.sqx.modules.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.goods.dao.GoodsSkuDao;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.entity.GoodsSku;
import com.sqx.modules.goods.service.GoodsSkuService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Admin
* @description 针对表【goods_sku(租赁物品的规格表)】的数据库操作Service实现
* @createDate 2024-10-09 14:49:21
*/
@Service
public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuDao, GoodsSku>
    implements GoodsSkuService {

    @Override
    public Result saveGoodsSku(GoodsSku goodsSku) {
        if (goodsSku.getId() !=null){
            goodsSku.setUpdateTime(new Date());
            baseMapper.updateById(goodsSku);
            return Result.success();
        }else {
            goodsSku.setCreateTime(new Date());
            baseMapper.insert(goodsSku);
            return Result.success();
        }
    }

    @Override
    public Result deleteGoodsSku(Long id) {
        GoodsSku goodsSku = baseMapper.selectById(id);
        if (goodsSku != null){
            baseMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("商品规格不存在");
    }

    @Override
    public Result selectGoodsSku(Long id) {
        GoodsSku goodsSku = baseMapper.selectById(id);
        if (goodsSku != null){
            return Result.success().put("data",goodsSku);
        }
        return Result.error("商品规格不存在");
    }

    @Override
    public IPage<GoodsSku> selectGoodsSkuList(Integer page, Integer limit, GoodsSku goodsSku) {
        Page<GoodsSku> pages;
        if (page != null && limit != null){
            pages = new Page<>(page, limit);
        }else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        QueryWrapper<GoodsSku> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(goodsSku.getDegree() != null,"degree","%"+goodsSku.getDegree()+"%");
        queryWrapper.like(goodsSku.getColor() != null,"color","%"+goodsSku.getColor()+"%");
        queryWrapper.like(goodsSku.getSize() != null,"size","%"+goodsSku.getSize()+"%");
        return baseMapper.selectPage(pages,queryWrapper);
    }
}




